import React from 'react'
import { Redirect, Route } from 'react-router'
import { FC } from 'react-router/node_modules/@types/react'
import { UserData } from '../../consts/auth'
import { isAuth } from '../../helpers/auth'
interface PrivateUserRouteProps {
    path:string,
    component: React.ComponentType<any>
}
const PrivateUserRoute : FC<PrivateUserRouteProps> = ({
    component: Component,
    ...rest
}) => {
    return (
        <Route {...rest} render={
            (props) => {
                const userData = isAuth();
                if( userData){
                    const {
                        user
                    } = userData as UserData;
                    if( user.role === 0 ){
                        return <Component {...props} />
                    } else {
                        return <Redirect to="/admin/dashboard" />

                    }
                } else {
                    return <Redirect to="/signin" />
                }
            }
        } />
            
    )
}

export default PrivateUserRoute
